package dao

import (
	"database/sql"
	"errors"
	"tcp_server/src/config"
	"tcp_server/src/models"
)

func GetTokenByUserName(username string) (*models.Token, error) {
	query := `
        SELECT token, expire_time
        FROM t_token 
        WHERE user_name = ?
    `

	row := config.Mysql.QueryRow(query, username)

	token := &models.Token{}
	err := row.Scan(&token.Token, &token.ExpireTime)
	if err != nil {
		if errors.Is(err, sql.ErrNoRows) {
			return nil, nil
		}
		return nil, err
	}
	return token, nil
}
